package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.order.OrderApplicationService;
import com.agileboot.attendance.domain.order.dto.OrderDTO;
import com.agileboot.attendance.domain.order.dto.OrderQuery;
import com.agileboot.attendance.domain.order.dto.OrderRequest;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.utils.poi.ExportImportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 订单管理Controller
 *
 * @author agileboot
 */
@Tag(name = "订单管理API")
@RestController
@RequestMapping("/attendance/order")
@RequiredArgsConstructor
public class OrderController extends BaseController {

    private final OrderApplicationService orderApplicationService;
    private final ExportImportService exportImportService;

    /**
     * 获取订单列表
     */
    @Operation(summary = "获取订单列表")
    @PreAuthorize("@permission.has('attendance:order:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<OrderDTO>> list(OrderQuery query) {
        PageDTO<OrderDTO> pageDTO = orderApplicationService.getOrderList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取指定项目的订单列表
     */
    @Operation(summary = "获取指定项目的订单列表")
    @PreAuthorize("@permission.has('attendance:order:list')")
    @GetMapping("/list/project")
    public ResponseDTO<PageDTO<OrderDTO>> listByProject(Long projectId, Integer pageNum, Integer pageSize) {
        OrderQuery query = new OrderQuery();
        query.setProjectId(projectId);
        query.setPageNum(pageNum);
        query.setPageSize(pageSize);
        PageDTO<OrderDTO> pageDTO = orderApplicationService.getOrderList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取订单详情
     */
    @Operation(summary = "获取订单详情")
    @PreAuthorize("@permission.has('attendance:order:query')")
    @GetMapping("/{orderId}")
    public ResponseDTO<OrderDTO> getInfo(@PathVariable Long orderId) {
        OrderDTO order = orderApplicationService.getOrderDetail(orderId);
        return ResponseDTO.ok(order);
    }

    /**
     * 新增订单
     */
    @Operation(summary = "新增订单")
    @PreAuthorize("@permission.has('attendance:order:add')")
    @PostMapping
    public ResponseDTO<Void> add(@Validated @RequestBody OrderRequest request) {
        orderApplicationService.addOrder(request);
        return ResponseDTO.ok();
    }

    /**
     * 修改订单
     */
    @Operation(summary = "修改订单")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @PutMapping
    public ResponseDTO<Void> edit(@Validated @RequestBody OrderRequest request) {
        orderApplicationService.updateOrder(request);
        return ResponseDTO.ok();
    }

    /**
     * 删除订单
     */
    @Operation(summary = "删除订单")
    @PreAuthorize("@permission.has('attendance:order:remove')")
    @DeleteMapping("/{orderId}")
    public ResponseDTO<Void> remove(@PathVariable Long orderId) {
        orderApplicationService.deleteOrder(orderId);
        return ResponseDTO.ok();
    }
    
    /**
     * 完成订单
     */
    @Operation(summary = "完成订单")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @PutMapping("/complete/{orderId}")
    public ResponseDTO<Void> complete(@PathVariable Long orderId) {
        orderApplicationService.completeOrder(orderId);
        return ResponseDTO.ok();
    }
    
    /**
     * 取消订单
     */
    @Operation(summary = "取消订单")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @PutMapping("/cancel/{orderId}")
    public ResponseDTO<Void> cancel(@PathVariable Long orderId) {
        orderApplicationService.cancelOrder(orderId);
        return ResponseDTO.ok();
    }
    
    /**
     * 导出订单数据
     */
    @Operation(summary = "导出订单数据")
    @PreAuthorize("@permission.has('attendance:order:export')")
    @GetMapping("/export")
    public ResponseDTO<Void> export(OrderQuery query, HttpServletResponse response) throws IOException {
        List<OrderDTO> list = orderApplicationService.getOrderListForExport(query);
        exportImportService.exportExcel(list, OrderDTO.class, response, "订单数据");
        return ResponseDTO.ok();
    }

    /**
     * 下载订单导入模板
     */
    @Operation(summary = "下载订单导入模板")
    @PreAuthorize("@permission.has('attendance:order:import')")
    @GetMapping("/importTemplate")
    public ResponseDTO<Void> importTemplate(HttpServletResponse response) throws IOException {
        exportImportService.exportTemplateExcel(OrderDTO.class, response, "订单数据导入模板");
        return ResponseDTO.ok();
    }

    /**
     * 导入订单数据
     */
    @Operation(summary = "导入订单数据")
    @PreAuthorize("@permission.has('attendance:order:import')")
    @PostMapping("/importData")
    public ResponseDTO<Void> importData(MultipartFile file) throws IOException {
        orderApplicationService.importOrderData(file);
        return ResponseDTO.ok();
    }
} 